/*
 * @(#)BuildTreeMenu.java 
 *
 * Project: HiTRUST Java Utilities Project
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   HZ Hitrust     2005/12/05  first release
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2005 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.util.menu;
import java.util.ArrayList;
import com.hitrust.gs.bean.GSFunctionBean;
import com.hitrust.gs.dao.GSFunctionDAO;
import com.hitrust.gs.lib.GSLogWriter;
public class BuildTreeMenu {
	    //菜单数据缓存对象
		private static ArrayList sTreeMenu = null;
	    
	    //单一实例
	    private static BuildTreeMenu sBuildTreeMenu = null;
	    
	    /**
	     * 私有构造方法
	     */
	    private BuildTreeMenu() throws Exception {
	        try {
	            sTreeMenu = new ArrayList();

	            Label tLabel; // 标签
	            Menu tMenu; // 一级菜单
	            Item tItem; // 菜单项
	            GSFunctionBean tFunctionBean ;
	            
	            //取得所有标签类型的功能
	            ArrayList tLabels = new GSFunctionDAO().getFunctions("0");
	            if (tLabels.size() == 0){
	                throw new Exception("系统功能初始化异常，未读取到任何标签！");
	            }
	            
	            //取得所有的一级菜单类型的功能（一级菜单或者功能项）
	            ArrayList tOneMenus = new GSFunctionDAO().getFunctions("1");
	            if (tOneMenus.size() == 0){
	                throw new Exception("系统功能初始化异常，未读取到任何[一级菜单或者功能项]！");
	            }
	            
	            //取得所有的二级菜单类型的功能（二级菜单或者功能项）
	            ArrayList tTwoMenus = new GSFunctionDAO().getFunctions("2");
	            if (tTwoMenus.size() == 0){
	                //throw new Exception("系统功能初始化异常，未读取到任何[二级菜单或者功能项]！");
	                //不判断,因为可能所有的功能都是一级菜单
	            }
	            
	            //开始循环对取得的标签进行处理
	            for (int i = 0; i < tLabels.size(); i++){
	                tLabel = new Label();
	                tFunctionBean = (GSFunctionBean)tLabels.get(i);
	                
	                //为标签赋值
	                tLabel.setFuncCode(tFunctionBean.getGs_func_id().trim());
	                tLabel.setFuncName(tFunctionBean.getGs_func_name().trim());
	                tLabel.setFuncType(tFunctionBean.getGs_func_type().trim());
	                tLabel.setMenuLevel(tFunctionBean.getGs_menu_level().trim());
	                tLabel.setParentFuncCode(tFunctionBean.getGs_parent_func_id().trim());
	                
	                //取出标签下属的一级菜单容器-备用
	                ArrayList tLabelSubMenuBeans = tLabel.getSubMenuBeans();
	                
	                //开始循环对取得的一级菜单进行处理
	                for (int j = 0; j < tOneMenus.size(); j++){
	                    tMenu = new Menu();
	                    tFunctionBean = (GSFunctionBean)tOneMenus.get(j);
	                    //如果是当前标签的菜单
	                    if (tFunctionBean.getGs_parent_func_id().trim().equals(tLabel.getFuncCode())){
	                        //为一级菜单赋值
	                        tMenu.setFuncCode(tFunctionBean.getGs_func_id().trim());
	                        tMenu.setFuncName(tFunctionBean.getGs_func_name().trim());
	                        tMenu.setFuncType(tFunctionBean.getGs_func_type().trim());
	                        tMenu.setMenuLevel(tFunctionBean.getGs_menu_level().trim());
	                        tMenu.setParentFuncCode(tFunctionBean.getGs_parent_func_id().trim());
	                        
	                        //如果该菜单是具体的功能项
	                        if (tMenu.getFuncType().equals("2")){
	                            tLabelSubMenuBeans.add(tMenu);//添加到标签下属的一级菜单列表中
	                        }
	                        //如果该菜单下面还有二级菜单
	                        else {
	                            //取出一级菜单下属的二级菜单容器-备用
	                            ArrayList tOneSubMenuBeans = tMenu.getSubMenuBeans();
	                            
	                            //开始循环对取得的二级菜单进行处理
	                            for (int k = 0; k < tTwoMenus.size(); k++){
	                                tItem = new Item();
	                                tFunctionBean = (GSFunctionBean)tTwoMenus.get(k);
	                                if (tFunctionBean.getGs_parent_func_id().trim().equals(tMenu.getFuncCode())){
	                                    //为 二级菜单-具体功能项 赋值
	                                    tItem.setFuncCode(tFunctionBean.getGs_func_id().trim());
	                                    tItem.setFuncName(tFunctionBean.getGs_func_name().trim());
	                                    tItem.setFuncType(tFunctionBean.getGs_func_type().trim());
	                                    tItem.setMenuLevel(tFunctionBean.getGs_menu_level().trim());
	                                    tItem.setParentFuncCode(tFunctionBean.getGs_parent_func_id().trim());
	                                    tOneSubMenuBeans.add(tItem);//添加到一级菜单下属的二级菜单列表中
	                                }
	                            }
	                            if (tOneSubMenuBeans != null && tOneSubMenuBeans.size() > 0){
	                                tLabelSubMenuBeans.add(tMenu);//添加到标签下属的一级菜单列表中
	                            }
	                        }
	                    }
	                }
	                
	                if (tLabelSubMenuBeans != null && tLabelSubMenuBeans.size() > 0){
	                    sTreeMenu.add(tLabel);//添加到菜单树中
	                }
	            }
	        } catch (Exception e) {
	            sTreeMenu = null;
	            sBuildTreeMenu = null;
	        }
	    }

	    /**
	     * 取得对象单一实例
	     * 
	     * @return BuildTreeMenu
	     */
	    public static final synchronized BuildTreeMenu getInstance(){
	        if (sBuildTreeMenu == null){
	            try {
	                sBuildTreeMenu = new BuildTreeMenu();
	            } catch (Exception e) {
	                GSLogWriter.fatal(e.getMessage());
	            }
	        }
	        
	        return sBuildTreeMenu;    
	    }

	    
	    /**
	     * 输出菜单树信息，用于测试
	     * 
	     * @param aTreeMenu
	     */
	    public void printTreeMenu(ArrayList aTreeMenu) {
	        for (int i = 0; i < aTreeMenu.size(); i++) {
	            Label tLabel = (Label)aTreeMenu.get(i);
	            GSLogWriter.debug(this,"[Label]function_code:" + tLabel.getFuncCode() + "[0]label:" + tLabel.getFuncName());
	            ArrayList tMenus = tLabel.getSubMenuBeans();
	            Menu tMenu = null;
	            for (int j = 0; j < tMenus.size(); j++) { 
	                tMenu = (Menu) tMenus.get(j);
	                GSLogWriter.debug(this,"-[Menu]function_code:" + tMenu.getFuncCode() + "[1]menu:" + tMenu.getFuncName());
	                ArrayList tItems = tMenu.getSubMenuBeans();
	                Item tItem = null;
	                for (int k = 0; k < tItems.size(); k++) {
	                    tItem = (Item) tItems.get(k);
	                    GSLogWriter.debug(this,"--[Item]function_code:" + tItem.getFuncCode() + "[2]item:" + tItem.getFuncName());
	                }
	            }

	        }
	    }

	    /**
	     * 用用户拥有操作权限的功能代码过滤系统模板菜单树，生成用户有权操作的菜单树
	     * 
	     * @param String[] aFunctionCodes 用户拥有操作权限的功能代码
	     * @return ArrayList 用户有权操作的菜单树
	     */
	    public ArrayList filterTreeMenu(String[] aFunctionCodes) {
	        //克隆一份公共缓存菜单树
	        ArrayList tResultTreeMenu = getTreeMenu();

	        for (int i = 0; i < tResultTreeMenu.size(); i++){
	            Label tLabel = (Label)tResultTreeMenu.get(i);
	            ArrayList tMenus = tLabel.getSubMenuBeans();
	            Menu tMenu = null;
	            for (int j = 0; j < tMenus.size(); j++){
	                tMenu = (Menu) tMenus.get(j);
	                ArrayList tItems = tMenu.getSubMenuBeans();
	                Item tItem = null;
	                //菜单项、菜单过滤
	                for (int k = 0; k < tItems.size(); k++) {
	                    tItem = (Item) tItems.get(k);
	                    // 菜单项过滤
	                    boolean tIsNeedDel = true;
	                    for (int m = 0; m < aFunctionCodes.length; m++) {
	                        if (tItem.getFuncCode().equals(aFunctionCodes[m])) { // 该菜单项在功能代码列表中
	                            tIsNeedDel = false;
	                            break;
	                        }
	                    }
	                    if (tIsNeedDel) {//去掉没有权限的菜单项
	                        tItems.remove(k);
	                        k--;
	                    }
	                }
	                
	                if (tItems.size() == 0 && tMenu.getFuncType().equals("1")){//去掉没有功能项的菜单
	                    tMenus.remove(j);
	                    j--;
	                }
	                
	                //去掉没有权限的 一级菜单-具体功能项
	                if (tMenu.getFuncType().equals("2")){
	                    boolean tIsNeedDel = true;
	                    for (int m = 0; m < aFunctionCodes.length; m++) {
	                        if (tMenu.getFuncCode().equals(aFunctionCodes[m])) { //该菜单项在功能代码列表中
	                            tIsNeedDel = false;
	                            break;
	                        }
	                    }
	                    if (tIsNeedDel) {//去掉没有权限的菜单项
	                        tMenus.remove(j);
	                        j--;
	                    }
	                }
	            }

	            if (tMenus.size() == 0){//去掉没有菜单项的标签
	                tResultTreeMenu.remove(i);
	                i--;
	            }
	        }

	        return tResultTreeMenu;
	    }

	    /**
	     * 取得一份菜单树的拷贝
	     * 
	     * @return ArrayList
	     */
	    public ArrayList getTreeMenu(){
	        //克隆一份公共缓存菜单树
	        ArrayList tResultTreeMenu = new ArrayList();
	        for (int i=0; i<sTreeMenu.size(); i++){
	            tResultTreeMenu.add(((MenuBean)sTreeMenu.get(i)).clone());
	        }
	        return tResultTreeMenu;
	    }





	    public static void main(String[] args) {
	        System.setProperty("cupsecure.runtime.mode", "unitest");
	        BuildTreeMenu tBuildTreeMenu = BuildTreeMenu.getInstance();
	        ArrayList tTreeMenu = tBuildTreeMenu.getTreeMenu();
	        
	        tBuildTreeMenu.printTreeMenu(tTreeMenu);
	       
	    }
	
}